
/*-----------------------
Table 4 -- Domestic Prices
-----------------------*/

/*-------------------------
I. Prepare Data
--------------------------*/

loc run = 1
if `run' == 1{

use ..\data\icp_ap_clean.dta, clear
tsset id year, delta(6) yearly

egen product = group(ItemCode)

merge m:1 countrycode year using ..\temp\wdi_inflation.dta
drop if _m==2
drop _m

gen bh = substr(ItemCode,1,7)

codebook bh
codebook ItemCode

rename year y
rename countrycode c
merge m:1 c y bh using "$data\EDD importer concentration measures 2024 March\Indicators based on all observations\CYICPproducts_importerconcentration.dta"
drop if _m==2
drop _m

merge m:1 y c using "$data\gini.dta"
drop if _m==2
drop _m

merge m:1 y c using "$data\wdi_v2.dta"
drop if _m==2
drop _m

rename y year
rename c iso3code

rename iso3code code
merge m:1 code year using "$data\wgidataset"
drop if _m==2
drop _m
rename code iso3code

merge m:1 iso3code year using "$data\commoshockfixedw"
drop if _m==2
drop _m
rename commshockfixw compriceindexfixw

rename year y
rename iso3code c

//*Price differences
sort id y
gen ap_ln = ln(ap)
gen FXwdi_ln = ln(FXwdi)

replace B1 = B1*10000
gen hhi_ln = ln(B1)

//*Demean
foreach var of varlist commodity_exports fuel_exports ores_metal_exports food_exports {
		replace `var' = `var'/100
		sum `var',d
		replace `var' =(`var'-r(mean))
}

egen cy = group(c y)

label var ap_ln "Log local price"
label var hhi_ln "Log HHI"
label var FXwdi_ln "Log nominal exchange rate with US"
label var loggdppc "Log GDPppp per capita"
label var loggdppc2 "Log GDP per capita"
lab var commodity_exports "Commodity export share [0,1]"
lab var compriceindexfixw "Export commodity price index (1=2012)"

egen bhy = group(bh y) /*product 7 digit ICP code*/
egen producty = group(product y) /* product 9 digit  ICP code*/

save "$temp/price_data.dta", replace

}


/*-------------------------
II. Product Level Regressions
--------------------------*/

loc run = 1
if `run' == 1{

log using ..\output\logfiles\Table_4.smcl, replace

use ..\temp/price_data.dta, clear

reghdfe ap_ln hhi_ln FXwdi_ln, absorb(producty) cluster(bhy)
sum  ap_ln   if e(sample)==1
loc m = r(mean)
outreg2 using ..\output/table4_price.tex, tex(frag) replace label  nocons dec(2) pdec(2) addtext("Product-year fixed effects", "Yes")    addstat(Dependent variable mean, `m')

reghdfe ap_ln  hhi_ln  FXwdi_ln cce rqe loggdppc gini_i, absorb(producty) cluster(bhy)
sum  ap_ln   if e(sample)==1
loc m = r(mean)
outreg2 using ..\output/table4_price.tex, tex(frag)  append label  nocons dec(2) pdec(2) addtext("Product-year fixed effects", "Yes")   addstat(Dependent variable mean, `m')

codebook bh // Count of basic headings for footnote
codebook product // Count of products for footnote

log close
}